$.fn.extend({
	enable : function(enable) {
		return enable ? $(this).removeAttr('disabled') : $(this).attr(
				'disabled', true);
	}
})

/*
 * 录制的逻辑控制
 */
$(function() {
	var recording = false;
	var currentInfoNode;
	var counter;
	var startTime;
	var length;
	var topicTitleInput;
	var storyId = 0;
	var storyData;
	var currentId = 0;
	var user = yy.user.getCurrentUserInfo() || {
		name : 'test'
	};
	var playing = false;
	var player = $('<div/>').jPlayer({
		ended : function() {

		},
		swfPath : './'
	});

	function play() {
		if (playing)
			return;
		var file = 'Sequence?id=' + storyData[currentId].file;
		player.jPlayer('setMedia', {
			mp3 : file
		}).jPlayer('play');
		playing = true;
	}

	function pause() {
		if (playing) {
			player.jPlayer('pause');
			playing = false;
		}
	}

	$('#play-button').click(function() {
		playing ? pause() : play();
	});

	function nextRandom() {
		$.ajax({
			url : 'Story/Random',
			contentType : 'application/json',
			success : function(data) {
				eval("data = " + data);
				loadStory(data.id);
			}
		});
	}

	nextRandom();

	function setRecording(val) {
		(recording = val) ? $('#record-button').addClass('recording') : $(
				'#record-button').removeClass('recording');
	}

	function startRecordCounter() {
		counter = setInterval(function() {
			var now = new Date();
			length = Math.floor((now.getTime() - startTime.getTime()) / 1000),
					minute = Math.floor(length / 60), sec = length % 60;

			currentInfoNode.text('普通模式下，请按F2录制，自由模式直接录制。当前已采集：' + length
					+ ' / 60 秒');
			if (length > 60 * 1000)
				stopRecord();
		}, 1000);
	}

	function onRecordStart() {
		setRecording(true);
		currentInfoNode = $('<p></p>').appendTo('#record-area');
		startTime = new Date();
		startRecordCounter();
	}

	function onRecordFinished(e) {
		setRecording(false);
		currentInfoNode.text('正在上传您的录音，请稍后...');
		var iframe = $('#hidden_frame');
		var form = $('<form></form>').attr('target', 'hidden_frame').appendTo(
				'body').css('position', 'absolute').css('top', '100000px');
		form.attr('method', 'POST').attr('enctype', 'multipart/form-data')
				.attr('action', 'Story/Topic');
		$('<input/>').attr('name', 'follow').val(storyId).appendTo(form);
		$('<input/>').attr('name', 'uid').val(user.uid).appendTo(form);
		$('<input/>').attr('name', 'length').val(length).appendTo(form);
		$('<input/>').attr('name', 'name').val(
				topicTitleInput ? topicTitleInput.val() : 'reply').appendTo(
				form);
		$('<input/>').attr('name', 'fileupload').attr('type', 'file').val(
				e.filename).appendTo(form);
		$('<input/>').attr('type', 'submit').val('Submit').appendTo(form);
		form[0].submit();
		iframe.onload = function() {
			var ret = eval(iframe.innerHTML);
			loadStory(ret.id);
		}
	}

	function getUser(uid) {
		var user = yy.user.getUserInfo(uid);
		return user ? user : {
			name : 'test'
		};
	}

	function processStoryData(data) {
		eval("data = " + data);
		storyData = data;
		var con = $('#record-area');
		con.empty();
		for ( var i = 0; i < data.length; i++) {
			var uid = data[i].uid, file = data[i].file, flower = data[i].flower, egg = data[i].egg, length = data[i].length;
			$('<h3/>').text(getUser(uid).name).appendTo(con);
			$('<p />').text('共 ' + length + ' 秒的凶铃').appendTo(con);
		}
	}

	function loadStory(id) {
		storyId = id;
		$.ajax({
			url : 'Story/Sequences?id=' + id,
			contentType : 'application/json',
			success : function(data) {
				processStoryData(data);
			}
		});
	}

	function startRecord() {
		yy.audio.startRecord("test.mp3");
		onRecordStart();
	}

	function stopRecord() {
		yy.audio.stopRecord();
		clearInterval(counter);
	}

	$('#record-button').click(function(e) {
		recording ? stopRecord() : startRecord();
	});

	function getUserHead(uid, type) {
		return 'img/user-head-' + type + '.png';
	}

	function newTopic() {
		!user && (user = {
			name : 'techird'
		});
		$('#author-info').html('');
		$('<a/>').text(user.name).appendTo('#author-info');
		$('<img/>').attr('src', getUserHead(user.uid, 'large')).appendTo(
				'#author-info');
		$('#topic-title h2').empty();
		topicTitleInput = $('<input/>').attr('type', 'text').val(
				'新故事 by ' + user.name).width(200).appendTo('#topic-title h2');
		$('#record-area').html('马上点击录音按钮录制故事的第一段吧！');
	}

	$('#new-topic').click(newTopic);

	$('#user').html(user.name);
	yy.audio.addEventListener(IYYAudio.RECORD_FINISHED, onRecordFinished);
});

/**
 * UI控制
 */
$(function() {
	$('#list-slide a').click(function(e) {
		$('#story-list').toggleClass('expanded');
	});
	$('#play-progress').slider({
		range : 'min',
		value : 37,
		min : 1,
		max : 100,
		slide : function() {

		}
	});
});
